﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFMysqlConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer<MyContext>(null);

            //CreateUser();

            //CreateProduct();

            //CreateOrder();

            QueryOrder();

            Console.WriteLine(DateTime.Now.ToString());
            Console.ReadLine();
        }

        static void CreateUser()
        {
            using (var context = new MyContext())
            {
                context.Users.Add(new User { UserName = "张三" });
                var result = context.SaveChanges();

                Console.WriteLine("创建用户结果{0}", result);
            }
        }

        static void CreateProduct()
        {
            using (var context = new MyContext())
            {
                context.Products.Add(new Product
                {
                    ProductName = "Apple Iphone 6",
                    Price = 5288
                });

                context.Products.Add(new Product
                {
                    ProductName = "冰箱",
                    Price = 3888
                });

                context.Products.Add(new Product
                {
                    ProductName = "空调",
                    Price = 8888
                });
                var result = context.SaveChanges();

                Console.WriteLine("创建商品结果{0}", result);
            }
        }

        static void CreateOrder()
        {
            using (var context = new MyContext())
            {
                var order = new Order()
                {
                    UserId = 1,
                    OrderCode = Guid.NewGuid().ToString()
                };

                order.AddItem(new OrderItem()
                {
                    ProductId = 1,
                    Quantity = 2,
                    Price = 5288,
                    Amount = 2 * 5288,
                });

                order.AddItem(new OrderItem()
                {
                    ProductId = 3,
                    Quantity = 5,
                    Price = 8888,
                    Amount = 5 * 8888,
                });

                context.Orders.Add(order);

                var result = context.SaveChanges();

                Console.WriteLine("创建订单结果{0}", result);
            }
        }

        static void QueryOrder()
        {
            using (var context = new MyContext())
            {
                var order = context.Orders.SingleOrDefault(item => item.Id == 2);

                if (order == null) return;

                Console.WriteLine("订单编号：{0}", order.OrderCode);
                Console.WriteLine("订单总数量：{0}", order.Quantity);
                Console.WriteLine("订单总金额：{0}", order.Amount);
                foreach (var item in order.OrderItems)
                {
                    Console.WriteLine("     订单明细Id：{0}", item.Id);
                    Console.WriteLine("     订单明细商品：{0}", item.Product.ProductName);
                    Console.WriteLine("     订单明细数量：{0}", item.Quantity);
                    Console.WriteLine("     订单明细单价：{0}", item.Price);
                    Console.WriteLine("     订单明细总价：{0}", item.Amount);
                    Console.WriteLine(" =============================");
                }
                
                Console.WriteLine("订单用户：{0}", order.User.UserName);
            }
        }
    }
}
